import Nexus = require("@nexus/schema");
import types from "./types";
import log from "../util/plugin/log";

import connection from "../util/plugin/connection";
import path from "path";

import { DateTimeResolver, JSONObjectResolver, TimeResolver } from "graphql-scalars";

const jsonScalar = Nexus.asNexusMethod(JSONObjectResolver, "json");
const dateTimeScalar = Nexus.asNexusMethod(DateTimeResolver, "date");
const timeScalar = Nexus.asNexusMethod(TimeResolver, "time");

const schema = Nexus.makeSchema({
  types: [...types, jsonScalar, dateTimeScalar, timeScalar],
  outputs: {
    schema: __dirname + "/../../schema.graphql",
    typegen: __dirname + "/../../typings.ts",
  },
  typegenAutoConfig: {
    sources: [
      {
        source: path.join(__dirname, "path", "to", "contextModule", "contextModule.ts"),
        alias: "t",
      },
    ],
    contextType: "t.Context",
  },
  plugins: [
    log,
    Nexus.fieldAuthorizePlugin({
      formatError: (error) => {
        return error.error;
      },
    }),
    Nexus.declarativeWrappingPlugin(),
    connection,
  ],
});

export default schema;
